
#include <stdlib.h>
#include <string.h>
#include "static-linked-list.h"


int InitList(StcLiNode space[]){
    for (int i = 0; i < MAXSIZE - 1; ++i) {
        space[i].cur = i+1;
    }
    space[MAXSIZE-1].cur = 0;
    return(0);
}

unsigned int Malloc_SLL(StcLiNode space[]){
    unsigned int i = space[0].cur;
    if (space[0].cur){
        space[0].cur = space[i].cur;
    }
    return(i);
}

int Free_SLL(StcLiNode space[],unsigned int i){
    space[i].cur = space[0].cur;
    space[0].cur = i;
    return(0);
}

int ListInsert(StcLiNode space[],unsigned int i,ElemType e){
    if (i == 0 || i >= MAXSIZE-1){
        return(-1);
    }else if (i == 1){
        unsigned int k = Malloc_SLL(space);
        memcpy(&space[k].data,&e,sizeof(ElemType));
        space[k].cur = space[MAXSIZE-1].cur;
        space[MAXSIZE-1].cur = k;
        return(0);
    }else{
        unsigned current = space[MAXSIZE-1].cur;
        for (int j = 2; j < i; ++j) {
            if (space[current].cur == 0){
                space[current].cur = Malloc_SLL(space);
                memset(&space[space[current].cur].data,0,sizeof(ElemType));
                space[space[current].cur].cur = 0;
            }
            current = space[current].cur;
        }
        unsigned int tmp = space[current].cur;
        space[current].cur = Malloc_SLL(space);
        current = space[current].cur;
        space[current].cur = tmp;
        memcpy(&space[current].data,&e, sizeof(ElemType));
        return(0);
    }

}

int ListDelete(StcLiNode space[],unsigned int i,ElemType *e){
    if (i == 0 || space[MAXSIZE-1].cur == 0){
        return(-1);
    }else if (i == 1){
        unsigned int tmp = space[MAXSIZE-1].cur;
        space[MAXSIZE-1].cur = space[tmp].cur;  //!!order
        Free_SLL(space,tmp);
        return(0);
    }else{
        unsigned int current = space[MAXSIZE-1].cur,tmp;
        for (int j = 2; j < i; ++j) {
            if (space[current].cur == 0){
                return(-1);
            }
            current = space[current].cur;
        }
        tmp = space[current].cur;
        space[current].cur = space[tmp].cur;
        memcpy(e,&space[tmp].data, sizeof(ElemType));
        Free_SLL(space,tmp);
        return(0);
    }
}

int LocateElem(StcLiNode space[],ElemType e){
    unsigned int current = space[MAXSIZE-1].cur,i = 0;
    while (current != 0){
        ++i;
        if (memcmp(&space[current].data,&e, sizeof(ElemType)) == 0){
            return (int)i;
        }
        current = space[current].cur;
    }
    return(0);
}

int GetElem(StcLiNode space[],unsigned int i,ElemType *e){
    unsigned int current = space[MAXSIZE-1].cur;
    for (int j = 1; j < i; ++j) {
        if (current == 0){
            return(-1);
        }
        current = space[current].cur;
    }
    memcpy(e,&space[current].data, sizeof(ElemType));
    return(0);
}

int ClearList(StcLiNode space[]){
    memset(space,0,MAXSIZE* sizeof(StcLiNode));
    return(0);
}

int ListEmpty(StcLiNode space[]){
    if (space[MAXSIZE-1].cur == 0){
        return(1);
    } else{
        return(0);
    }
}